package GUI.Cliente;

import java.rmi.RemoteException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Cliente.Exceptions.Confirmar;
import client.entities.ClientVO;
import client.exceptions.BadClientException;
import client.exceptions.IncompleteClientException;
import client.exceptions.MissingClientException;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import dbExceptions.DataBaseErrorException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.client.interfaces.ClientProxyMgt;

public class DatosCliente extends Dialog {

	protected Object result;
	protected Shell shell;
	ClientVO c;
	int op;
	//op=0 agregar, op=1 modificar, op=2 eliminar, op==3 ver
	/**
	 * Create the dialog
	 * @param parent
	 * @param style
	 */
	public DatosCliente(Shell parent, int style, ClientVO cliente, int opcion) {
		super(parent, style);
		c=cliente;
		op=opcion;
		setText("Datos el Cliente");
	}

	/**
	 * Open the dialog.
	 * @return the result
	 */
	public Object open() {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(441, 407);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel = new FormData();
		fd_lblNewLabel.top = new FormAttachment(0, 40);
		fd_lblNewLabel.left = new FormAttachment(0, 25);
		lblNewLabel.setLayoutData(fd_lblNewLabel);
		lblNewLabel.setText("Nombre");
		
		Label lblNewLabel_1 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_1 = new FormData();
		fd_lblNewLabel_1.top = new FormAttachment(0, 80);
		fd_lblNewLabel_1.left = new FormAttachment(0, 25);
		lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
		lblNewLabel_1.setText("Apellido");
		
		Label lblNewLabel_2 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_2 = new FormData();
		fd_lblNewLabel_2.top = new FormAttachment(0, 120);
		fd_lblNewLabel_2.left = new FormAttachment(0, 25);
		lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
		lblNewLabel_2.setText("Cedula de Identidad");
		
		Label lblNewLabel_3 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_3 = new FormData();
		fd_lblNewLabel_3.top = new FormAttachment(0, 160);
		fd_lblNewLabel_3.left = new FormAttachment(0, 25);
		lblNewLabel_3.setLayoutData(fd_lblNewLabel_3);
		lblNewLabel_3.setText("Direccion");
		
		Label lblNewLabel_4 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_4 = new FormData();
		fd_lblNewLabel_4.top = new FormAttachment(0, 200);
		fd_lblNewLabel_4.left = new FormAttachment(0, 25);
		lblNewLabel_4.setLayoutData(fd_lblNewLabel_4);
		lblNewLabel_4.setText("Telefono Fijo");
		
		Label label = new Label(shell, SWT.BORDER | SWT.SEPARATOR);
		FormData fd_label = new FormData();
		fd_label.top = new FormAttachment(0, 36);
		fd_label.left = new FormAttachment(lblNewLabel_2, 6);
		fd_label.right = new FormAttachment(0, 214);
		label.setLayoutData(fd_label);
		
		Label lblNewLabel_5 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_5 = new FormData();
		fd_lblNewLabel_5.top = new FormAttachment(lblNewLabel_4, 28);
		fd_lblNewLabel_5.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		lblNewLabel_5.setLayoutData(fd_lblNewLabel_5);
		lblNewLabel_5.setText("Telefono Celular");
		
		Label lblNewLabel_6 = new Label(shell, SWT.NONE);
		FormData fd_lblNewLabel_6 = new FormData();
		fd_lblNewLabel_6.top = new FormAttachment(lblNewLabel_5, 30);
		fd_lblNewLabel_6.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		lblNewLabel_6.setLayoutData(fd_lblNewLabel_6);
		lblNewLabel_6.setText("Fecha de Nacimiento");
		
		Label lblNewLabel_7 = new Label(shell, SWT.NONE);
		fd_label.bottom = new FormAttachment(lblNewLabel_7, 0, SWT.BOTTOM);
		FormData fd_lblNewLabel_7 = new FormData();
		fd_lblNewLabel_7.top = new FormAttachment(lblNewLabel_6, 31);
		fd_lblNewLabel_7.left = new FormAttachment(lblNewLabel, 0, SWT.LEFT);
		lblNewLabel_7.setLayoutData(fd_lblNewLabel_7);
		lblNewLabel_7.setText("Fecha de inscripcion");
		
		Label nombre = new Label(shell, SWT.NONE);
		FormData fd_nombre = new FormData();
		fd_nombre.left = new FormAttachment(label, 6);
		fd_nombre.top = new FormAttachment(lblNewLabel, 0, SWT.TOP);
		nombre.setLayoutData(fd_nombre);
		nombre.setText(c.getName());
		
		Label apellido = new Label(shell, SWT.NONE);
		FormData fd_apellido = new FormData();
		fd_apellido.left = new FormAttachment(label, 6);
		fd_apellido.bottom = new FormAttachment(lblNewLabel_1, 0, SWT.BOTTOM);
		apellido.setLayoutData(fd_apellido);
		apellido.setText(c.getLastName());
		
		Label ci = new Label(shell, SWT.NONE);
		FormData fd_ci = new FormData();
		fd_ci.left = new FormAttachment(label, 6);
		fd_ci.bottom = new FormAttachment(lblNewLabel_2, 0, SWT.BOTTOM);
		ci.setLayoutData(fd_ci);
		ci.setText(String.valueOf(c.getClientId()));
		
		Label direccion = new Label(shell, SWT.NONE);
		FormData fd_direccion = new FormData();
		fd_direccion.left = new FormAttachment(label, 6);
		fd_direccion.bottom = new FormAttachment(lblNewLabel_3, 0, SWT.BOTTOM);
		direccion.setLayoutData(fd_direccion);
		direccion.setText(c.getAddress());
		
		Label telefonoFijo = new Label(shell, SWT.NONE);
		FormData fd_telefonoFijo = new FormData();
		fd_telefonoFijo.left = new FormAttachment(label, 6);
		fd_telefonoFijo.bottom = new FormAttachment(lblNewLabel_4, 0, SWT.BOTTOM);
		telefonoFijo.setLayoutData(fd_telefonoFijo);
		telefonoFijo.setText(c.getTel());
		
		Label telefonoCelular = new Label(shell, SWT.NONE);
		FormData fd_telefonoCelular = new FormData();
		fd_telefonoCelular.left = new FormAttachment(label, 6);
		fd_telefonoCelular.bottom = new FormAttachment(lblNewLabel_5, 0, SWT.BOTTOM);
		telefonoCelular.setLayoutData(fd_telefonoCelular);
		telefonoCelular.setText(c.getCel());
		
		Label fechaNacimiento = new Label(shell, SWT.NONE);
		FormData fd_fechaNacimiento = new FormData();
		fd_fechaNacimiento.left = new FormAttachment(label, 6);
		fd_fechaNacimiento.bottom = new FormAttachment(lblNewLabel_6, 0, SWT.BOTTOM);
		fechaNacimiento.setLayoutData(fd_fechaNacimiento);
		fechaNacimiento.setText(c.getBirthdate());

		Label fechaInscripcion = new Label(shell, SWT.NONE);
		FormData fd_fechaInscripcion = new FormData();
		fd_fechaInscripcion.left = new FormAttachment(label, 6);
		fd_fechaInscripcion.top = new FormAttachment(lblNewLabel_7, 0, SWT.TOP);
		fechaInscripcion.setLayoutData(fd_fechaInscripcion);
		fechaInscripcion.setText(c.getRegistrationDate().toString());
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		//Boton
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(op==1){
					shell.close();
				}
				if(op==3){
				
					shell.close();
				}
				if(op==0){
					String m = "CLIENTE AGRGADO CON EXITO";
					ClientProxyMgt uso = ServiceFacade.getInstance().getClientProxyMgt();
					try {
						
						uso.addClient(c);
						
						Confirmar conf = new Confirmar(shell, shell.getStyle(),m);
						shell.setVisible(false);
						conf.open();
						
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (IncompleteClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (MissingClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (DataBaseErrorException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (BadClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
					
					shell.close();
				}
				if(op==2){
					ClientProxyMgt uso = ServiceFacade.getInstance().getClientProxyMgt();
					try {
						uso.deleteClient(c);
						String m = "EL CLIENTE HA SIDO ELIMINADO DE LA BASE DE DATOS";
						Confirmar c = new Confirmar (shell, shell.getStyle(), m);
						shell.setVisible(false);
						c.open();
						
					} catch (RemoteException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (ProxyException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (MissingClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (DataBaseErrorException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					} catch (BadClientException e1) {
						ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
						ventana.open();
						shell.dispose();
						e1.printStackTrace();
					}
				}
			}
		});
		
		FormData fd_btnNewButton = new FormData();
		fd_btnNewButton.bottom = new FormAttachment(100);
		fd_btnNewButton.left = new FormAttachment(nombre, 0, SWT.LEFT);
		btnNewButton.setLayoutData(fd_btnNewButton);
		btnNewButton.setText("New Button");
		
		Button modNombre = new Button(shell, SWT.NONE);
		modNombre.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			String[] st = new String[3];
			st[0]= "Antiguo nombre";
			st[1] = c.getName();
			st[2] = "Nuevo Nombre";
			ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,0);
			shell.setVisible(false);
			mod.open();
			}
		});
		FormData fd_modNombre = new FormData();
		fd_modNombre.top = new FormAttachment(lblNewLabel, -7, SWT.TOP);
		fd_modNombre.right = new FormAttachment(100, -29);
		modNombre.setLayoutData(fd_modNombre);
		modNombre.setText("Modificar");
		
		Button modApellido = new Button(shell, SWT.NONE);
		modApellido.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Antiguo Apellido";
				st[1] = c.getLastName();
				st[2] = "Nuevo Apellido";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,1);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modApellido = new FormData();
		fd_modApellido.bottom = new FormAttachment(lblNewLabel_1, 0, SWT.BOTTOM);
		fd_modApellido.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modApellido.setLayoutData(fd_modApellido);
		modApellido.setText("Modificar");
		
		Button modCedula = new Button(shell, SWT.NONE);
		modCedula.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Cedula Guardada";
				st[1] = String.valueOf(c.getClientId());
				st[2] = "Nueva Cedula";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,2);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modCedula = new FormData();
		fd_modCedula.bottom = new FormAttachment(lblNewLabel_2, 0, SWT.BOTTOM);
		fd_modCedula.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modCedula.setLayoutData(fd_modCedula);
		modCedula.setText("Modificar");
		
		Button modDireccion = new Button(shell, SWT.NONE);
		modDireccion.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Antigua Direccion";
				st[1] = c.getAddress();
				st[2] = "Nueva Direccion";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,3);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modDireccion = new FormData();
		fd_modDireccion.bottom = new FormAttachment(lblNewLabel_3, 0, SWT.BOTTOM);
		fd_modDireccion.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modDireccion.setLayoutData(fd_modDireccion);
		modDireccion.setText("Modificar");
		
		Button modTel = new Button(shell, SWT.NONE);
		modTel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Antiguo Telefono";
				st[1] = c.getTel();
				st[2] = "Nuevo Telefonoe";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,4);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modTel = new FormData();
		fd_modTel.bottom = new FormAttachment(lblNewLabel_4, 0, SWT.BOTTOM);
		fd_modTel.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modTel.setLayoutData(fd_modTel);
		modTel.setText("Modificar");
		
		Button modCel = new Button(shell, SWT.NONE);
		modCel.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Antiguo Celular";
				st[1] = c.getCel();
				st[2] = "Nuevo Celular";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,5);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modCel = new FormData();
		fd_modCel.bottom = new FormAttachment(lblNewLabel_5, 0, SWT.BOTTOM);
		fd_modCel.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modCel.setLayoutData(fd_modCel);
		modCel.setText("Modificar");
		
		Button modBday = new Button(shell, SWT.NONE);
		modBday.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String[] st = new String[3];
				st[0]= "Antigua Fecha de Cumpleanos";
				st[1] = c.getBirthdate();
				st[2] = "Nuevo Fecha de Cumpleanos";
				ModificarAtributo mod = new ModificarAtributo (shell, shell.getStyle(),st,c,6);
				shell.setVisible(false);
				mod.open();
			}
		});
		FormData fd_modBday = new FormData();
		fd_modBday.bottom = new FormAttachment(lblNewLabel_6, 0, SWT.BOTTOM);
		fd_modBday.left = new FormAttachment(modNombre, 0, SWT.LEFT);
		modBday.setLayoutData(fd_modBday);
		modBday.setText("New Button");
		if (op==0 || op==2 || op==3){
			modNombre.setVisible(false);
			modApellido.setVisible(false);
			modTel.setVisible(false);
			modCel.setVisible(false);
			modCedula.setVisible(false);
			modDireccion.setVisible(false);
			modBday.setVisible(false);
		}
		
		if(op==0){
			btnNewButton.setText("Aceptar");
		}
		if(op==1){
			btnNewButton.setText("Aceptar");
			
		}
		if(op==2){
			btnNewButton.setText("Eliminar Cliente");
			
		}
		if(op==3){
			btnNewButton.setText("Aceptar");
		}
		
		

	}
}
